using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Compilation;
using System.CodeDom;
using System.Web.UI;
using AspNetExtensions.Helpers;

namespace AspNetExtensions.ExpressionBuilders
{

    /// <summary>
    /// An ExpressionBuilder for SSL Redirects
    /// </summary>
    /// <remarks>
    /// Use like this:
    /// <![CDATA[
    /// <asp:HyperLink runat="server" ID="_lnk" Text="SSL Page" NavigateUrl="<%$ HttpsUrl:~/ssl/default.aspx %>" />
    /// ]]>
    /// Don't forget to add the expressionBuilder to the web.config file.
    /// </remarks>
    [ExpressionPrefix("HttpsUrl")]
    public class HttpsUrlExpressionBuilder : ExpressionBuilder
    {
        public override CodeExpression GetCodeExpression(
            BoundPropertyEntry entry, 
            object parsedData,
            ExpressionBuilderContext context)
        {
            // boilerplate code to inject 'GetValue' in the page class
            CodeTypeReferenceExpression targetClass = new
            CodeTypeReferenceExpression(typeof(HttpsUrlExpressionBuilder));

            string targetMethod = "GetValue";
            CodeExpression methodParameter = new CodePrimitiveExpression(entry.Expression.Trim());

            return new CodeMethodInvokeExpression(targetClass, targetMethod, methodParameter);
        }



        public static object GetValue(string param)
        {
            return SslTools.GetAbsoluteUrl(param, ProtocolOptions.Https);
        }

    }


}
